package com.chat.server;

import com.chat.data.MySqlSingle;
import com.chat.entity.*;
import com.chat.mapper.RoomMapper;
import com.chat.mapper.UserJoinRoomFormMapper;
import com.chat.mapper.UserMapper;
import com.chat.util.JwtUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * roomServer 服务
 */
public class RoomServer implements DataPackagerServer {

    RoomMapper roomMapper;
    UserJoinRoomFormMapper userJoinRoomFormMapper;
    UserMapper userMapper;
    SqlSession sqlSession;

    public RoomServer() throws IOException {
        SqlSessionFactory sqlSessionFactory = MySqlSingle.getMySqlSingle().getSqlSessionFactory();
        sqlSession = sqlSessionFactory.openSession();
        roomMapper = sqlSessionFactory.getConfiguration().getMapper(RoomMapper.class,sqlSession);
        userJoinRoomFormMapper = sqlSessionFactory.getConfiguration().getMapper(UserJoinRoomFormMapper.class,sqlSession);
        userMapper = sqlSessionFactory.getConfiguration().getMapper(UserMapper.class,sqlSession);
    }

    @Override
    public DataPackage run(DataPackage source) {
        UserJoinRoomForm userJoinRoomForm;
        Map<String,Integer> param = new HashMap<>();
        switch (source.getAction()){
            case POST_ROOM_JOIN://申请加入聊天室
                userJoinRoomForm = (UserJoinRoomForm) source.getDate();
                if(userJoinRoomFormMapper.getSuccessfulCount(userJoinRoomForm.getRoomId(),userJoinRoomForm.getUserId())>0){
                    return DataPackage.failResponse("请勿重复加入聊天室");
                }
                userJoinRoomFormMapper.postJoinRoom(userJoinRoomForm);
                sqlSession.commit();
                return DataPackage.successResponse("加入成功!");

            case JUDGED_ROOM_FORM://审核加入聊天室表单
                param = (Map<String, Integer>) source.getDate();
                //获得用户信息
                String token = source.getToken();
                String judgeAccount = JwtUtils.getAccount(token);
                User user = userMapper.selectUserByAccount(judgeAccount);

                //获得申请表信息
                int formId = param.get("formId");
                int result = param.get("result");
                userJoinRoomForm = userJoinRoomFormMapper.selectFormById(formId);

                //管理员身份验证
                User evidenty = userMapper.selectAdmin(userJoinRoomForm.getRoomId(),user.getUserId());
                if(evidenty!=null){
                    //更新验证
                    userJoinRoomFormMapper.judgeForm(result,formId,user.getUserId());
                    sqlSession.commit();
                    return DataPackage.successResponse("修改成功！");
                }

                break;

            case GET_ROOM_FORMS://获取聊天室请求表单
                param = (Map<String, Integer>) source.getDate();

//                //获得用户信息
                token = source.getToken();
                judgeAccount = JwtUtils.getAccount(token);
                user = userMapper.selectUserByAccount(judgeAccount);

                //管理员身份验证
                evidenty = userMapper.selectAdmin(param.get("roomId"),user.getUserId());
                if(evidenty!=null){
                    List<UserJoinRoomForm> list = userJoinRoomFormMapper.selectFormsByRoom(param.get("roomId"));
                    DataPackage dataPackage = DataPackage.successResponse(list, ServerAction.YES);
                    return dataPackage;
                }
                break;

            case GET_ROOMS_BY_USER://获取聊天室信息
                token = source.getToken();
                String account = JwtUtils.getAccount(token);
                List<Room> rooms = roomMapper.selectRoomsByUser(account);
                return DataPackage.successResponse(rooms);

            case GET_ROOMS:
                rooms = roomMapper.selectRooms();
                return DataPackage.successResponse(rooms);
        }
        return DataPackage.failResponse("failed");
    }

}
